using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Commons;
using Datas;
using Mgrs;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;


public class ThisHandCard : ComMonoBehaviour,IPointerClickHandler
{
    public List<Herbal> thisHandCard = new List<Herbal>();
   // public List<int> thisCardId = new List<int>();
    public HerbalList HerbalList;

    public int id;
    public string cardName;

    public TextMeshProUGUI nameText;
    //草药卡图
    public Sprite thisHerbSprite;
    public Image thatHerbImage;
    //草药卡名字
    public Sprite thisHerbNameSprite;
    public Image thatHerbNameImage;

    //public HerbalList HerbalList;

    public bool isSelect;//判断是否被选中

    public GameObject HandPlace;//手牌区域游戏对象

    public int numberOfCardsInHand;
    
    public List<int> AllCardList;
    // Start is called before the first frame update
    void Start()
    {
        isSelect = false;
        AllCardList =PlayerData.GetCardIds();
        var bagCardIndex = Random.Range(0, AllCardList.Count);
        
        ChangeId(AllCardList[bagCardIndex]);
        AllCardList.RemoveAt(bagCardIndex);
        
        
    }

    // Update is called once per frame
    void Update()
    {
        
        nameText.text = "" + thisHandCard[0].HerbalTitle;
        
        thisHerbSprite = thisHandCard[0].HerbalImage;
        thatHerbImage.sprite = thisHerbSprite;

        id = thisHandCard[0].HerbalId;
        //thisHerbNameSprite = thisHandCard[0].herbNameImage;
        //thatHerbNameImage.sprite = thisHerbNameSprite;

    }

    public void ChangeId(int newCardId)
    {
        thisHandCard.Clear();
        thisHandCard.Add(HerbalList.MedicincCarLists[newCardId]);
        
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if (isSelect == false)
            {
                HandCardMgr.SelectCard(this.gameObject);
                HandCardMgr.SelectCardId(id);
                
                isSelect = true;
                SendMsg(MsgData.haveSelectHandCard);
                
                float x = this.transform.position.x;
                float y = this.transform.position.y;
                gameObject.transform.position = new Vector2(x , y+10);

            }else if (isSelect == true)
            {
                //theOutHandButton.SetActive(false);
                HandCardMgr.CancelSelectCard(this.gameObject);
                
                HandCardMgr.CancelSelectCardId(id);
                //SendMessageUpwards("CheckOutHandButtonNotActive");
                SendMsg(MsgData.haveNoSelectHandCard);
                isSelect = false;
                float x = this.transform.position.x;
                float y = this.transform.position.y;
                gameObject.transform.position = new Vector2(x , y-10);
            }
            
        }
    }

    protected override void OnBeforeDestroy()
    {
        
    }
}
